;
(function($, window, document, undefined) {
	var pluginName = "scrollBar";

	function ScrollBar($ele, opts) {
		var defaults = {
			container: null
		};
		this.scrollBar = $ele;
		this.options = $.extend({}, defaults, opts);
		this.initHandle();
	}
	ScrollBar.prototype = {
		constructor: ScrollBar,
		initHandle: function() {
			var navContainer = this.options.container,
				scrollBar = this.scrollBar,
				bar = scrollBar.find(".handle");
			if (!(navContainer && navList)) {
				console.error("navContainer or navList is null");
				return;
			}
			if (!(scrollBar.length > 0 && bar.length > 0)) {
				console.error("scrollBar or bar is not find");
				return;
			}
			//console.log(navContainer[0].scrollHeight,navContainer.height());
			if (navContainer[0].scrollHeight > navContainer.height()) {
				navContainer.css({
					width: "calc(100% + 20px)"
				});
				scrollBar.show();
				var navContainerH = navContainer.height(),
					barHandleH = parseInt(navContainerH / navContainer[0].scrollHeight * navContainerH);
				bar.height(this.setBarHandleHeight(barHandleH) + "px");
				this.eventHandle();
			} else {
			  navContainer.css({width: "100%"});
			  scrollBar.hide();
              this.cacleEventHandle();
			}
		},
		eventHandle: function() {
			var navContainer = this.options.container,
				scrollBar = this.scrollBar,
				bar = scrollBar.find(".handle"),
				navContainerH = navContainer.height(),
				barTop = 0,
				barScroll = false,
				initBarClickY = 0,
				barHandleH = parseInt(navContainerH / navContainer[0].scrollHeight * navContainerH),
				barDownEvent = null,
				barUpEvent = null,
				barMoveBase = 10;
				/*****滑轮滚动的处理******/
			navContainer.on("scroll", function() {
				if (navContainer[0].scrollTop < navContainer[0].scrollHeight - navContainerH) {
					barTop = navContainer[0].scrollTop / (navContainer[0].scrollHeight - navContainerH) * (navContainerH - barHandleH);
				} else {
					barTop = navContainer[0].scrollTop / (navContainer[0].scrollHeight - navContainerH) * (navContainerH - barHandleH) - 1;
				}
				bar.css({
					top: barTop + "px"
				});
			});
			navContainer.on("mousewheel", function() {
				bar.css({
					top: +"px"
				});
			});
			/*****按主滚动条时拖拽小条滚动的处理******/
			scrollBar.on("mousedown", function(e) {
				var clickY = e.clientY,
					scrollTop = navContainer[0].scrollTop,
					barHeight = bar.height(),
					barOffsetTop = bar.offset().top;
				//向下滚动
				if (clickY - barOffsetTop - barHeight > 0) {
					barDownEvent = setInterval(function() {
						barOffsetTop = bar.offset().top;
						if (clickY - barOffsetTop - barHeight > barMoveBase) {
							navContainer[0].scrollTop += barMoveBase;
						} else {
							navContainer[0].scrollTop += (clickY - barOffsetTop - barHeight);
							clearInterval(barDownEvent);
						}
					}, 10);
				}
				//向上滚动
				else if (barOffsetTop - clickY > 0) {
					barUpEvent = setInterval(function() {
						barOffsetTop = bar.offset().top;
						if (barOffsetTop - clickY > barMoveBase) {
							navContainer[0].scrollTop -= barMoveBase;
						} else {
							navContainer[0].scrollTop -= (barOffsetTop - clickY);
							clearInterval(barUpEvent);
						}
					}, 10);
				}
			});
			/*****按主拖拽小条时的滚动处理******/
			bar.on("mousedown", function(e) {
				barScroll = true;
				initBarClickY = e.clientY;
			});
			$(document).on("mousemove", function(e) {
				if (barScroll) {
					navContainer[0].scrollTop += e.clientY - initBarClickY;
					initBarClickY = e.clientY;
				}
			});
			$(document).on("mouseup", function() {
				barScroll = false;
			})
		},
		cacleEventHandle() {
			var navContainer = this.options.container,
				scrollBar = this.scrollBar,
				bar = scrollBar.find(".handle");
			navContainer.off("scroll");
			navContainer.off("mousewheel");
			/*****按主滚动条时拖拽小条滚动的处理******/
			scrollBar.off("mousedown");
			/*****按主拖拽小条时的滚动处理******/
			bar.off("mousedown");
			/*$(document).off("mousemove");
			$(document).off("mouseup");*/
		},
		setBarHandleHeight: function(barHandleH) {
			/*if(barHandleH<=15){
			  return 15;
			}
			else*/
			return barHandleH;
		}
	}
	$.fn[pluginName] = function(options) {
		var scrollBar = new ScrollBar($(this), options);
		return $(this);
	}
}(jQuery, window, document));